﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.BLL.Mtbl;
using System.Collections;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
namespace HKEA.CMS.Core.Models
{
    public class ContactPersonListDetail
    {
        public string Salutation { get; set; }
        public string FirstName_Eng { get; set; }
        public string LastName_Eng { get; set; }
        public string ChristinaName { get; set; }
        public string Position { get; set; }
        public string Position_Eng { get; set; }
        public string Position_Chi { get; set; }
        public int CPUID { get; set; }
        public tblContactPerson CPENT { get; set; }
        public string FullAdd_Eng { get; set; }
        public string FullAdd_Chi { get; set; }
        public int infoUID { get; set; }
        public tblContactInfo ContactInfo { get; set; }
        public string strSpace { get { return ",&nbsp;"; } }
        public string SourceType { get; set; }
        public int Deleted { get; set; }
        public string strCP
        {
            get
            {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(Salutation).Append(ChristinaName).Append("&nbsp;").Append(FirstName_Eng).Append("&nbsp;").Append(LastName_Eng);
                if (!string.IsNullOrEmpty(Position))
                    strBuilder.Append(strSpace).Append(Position);
                return strBuilder.ToString();
            }
        }
        public mtblArea areaENT { get; set; }
        public mtblCountry countryENT { get; set; }
        public string strAddress {
            get
            {
                string str = string.Empty;
                StringBuilder strBuilder = new StringBuilder();
                if (CPENT != null)
                {
                    var addressENT = CPENT.tblAddress;
                    if (addressENT != null)
                    {
                        countryENT = addressENT.mtblCountry;
                        if (addressENT.mtblCountry != null)
                        {
                            mtblArea area = new mtblArea();
                            mtblAreaBLL areaBLL = new mtblAreaBLL();
                            area.AreaUID = (int)addressENT.mtblCountry.AreaUID;
                            area.Deleted = 0;
                            areaENT = areaBLL.GetEntity(area);
                        }

                        
                        if (!string.IsNullOrEmpty(addressENT.Flat_Bldg_Eng))
                            strBuilder.Append(addressENT.Flat_Bldg_Eng).Append("&nbsp;");
                        if (!string.IsNullOrEmpty(addressENT.Street_Rd_Eng))
                            strBuilder.Append(addressENT.Street_Rd_Eng).Append("&nbsp;");
                        if (!string.IsNullOrEmpty(addressENT.Area_Eng))
                            strBuilder.Append(addressENT.Area_Eng).Append("&nbsp;");
                        if (!string.IsNullOrEmpty(addressENT.District_Eng))
                            strBuilder.Append(addressENT.District_Eng).Append("&nbsp;");

                        #region 英文 City、Province、Country过滤
                        if (!string.IsNullOrEmpty(addressENT.City_Eng) && !string.IsNullOrEmpty(addressENT.Province_Eng) && addressENT.City_Eng.ToLower().Trim() == addressENT.Province_Eng.ToLower().Trim())
                        {
                            strBuilder.Append(addressENT.City_Eng.Trim()).Append("&nbsp;");
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(addressENT.City_Eng))
                                strBuilder.Append(addressENT.City_Eng.Trim()).Append("&nbsp;");
                            if (!string.IsNullOrEmpty(addressENT.Province_Eng))
                                strBuilder.Append(addressENT.Province_Eng.Trim()).Append("&nbsp;");
                        }
                        if (countryENT != null)
                        {
                            string strCounrtyEng = countryENT.Country_Eng;
                            if (!string.IsNullOrEmpty(addressENT.Province_Eng) && !string.IsNullOrEmpty(strCounrtyEng) && addressENT.Province_Eng.ToLower().Trim() == strCounrtyEng.ToLower().Trim())
                            {
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(addressENT.Province_Eng) && !string.IsNullOrEmpty(strCounrtyEng) && !string.IsNullOrEmpty(addressENT.City_Eng) && strCounrtyEng.ToLower().Trim() == addressENT.City_Eng.ToLower().Trim())
                                {
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(strCounrtyEng))
                                    {
                                        strBuilder.Append(strCounrtyEng.Trim()).Append("&nbsp;");
                                    }
                                }
                            }
                        }
                        if (areaENT != null)
                        {
                            if (!string.IsNullOrEmpty(areaENT.Area_Eng))
                            {
                                strBuilder.Append(areaENT.Area_Eng.Trim()).Append("&nbsp;");
                            }
                        }
                        #endregion

                        strBuilder.Append("<br/>");

                        if (areaENT != null)
                        {
                            if (!string.IsNullOrEmpty(areaENT.Area_Chi))
                                strBuilder.Append(areaENT.Area_Chi);
                        }
                        string strCountryChi = string.Empty;
                        string CityChi = addressENT.City_Chi;
                        string ProvinceChi = addressENT.Province_Chi;
                        if (countryENT != null)
                        {
                            strCountryChi = countryENT.Country_Chi;
                        }



                        if (!string.IsNullOrEmpty(strCountryChi))
                        {
                            strBuilder.Append(strCountryChi.Trim());
                            if (!string.IsNullOrEmpty(ProvinceChi))
                            {
                                if (strCountryChi.ToLower().Trim() != ProvinceChi.ToLower().Trim())
                                {
                                    strBuilder.Append(ProvinceChi.Trim());
                                    if (!string.IsNullOrEmpty(CityChi) && CityChi.ToLower().Trim() != ProvinceChi.ToLower().Trim())
                                        strBuilder.Append(CityChi.Trim());
                                }
                            }
                            else //省份为空
                            {
                                if (!string.IsNullOrEmpty(CityChi) && CityChi.ToLower().Trim() != strCountryChi.ToLower().Trim())
                                    strBuilder.Append(CityChi.Trim());
                            }
                        }
                        else //国家为空
                        {
                            if (!string.IsNullOrEmpty(ProvinceChi))
                            {
                                strBuilder.Append(ProvinceChi.Trim());
                                if (!string.IsNullOrEmpty(CityChi) && CityChi.ToLower().Trim() != ProvinceChi.ToLower().ToLower())
                                {
                                    strBuilder.Append(CityChi.Trim());
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(CityChi))
                                    strBuilder.Append(CityChi.Trim());
                            }
                        }
                        if (!string.IsNullOrEmpty(addressENT.District_Chi))
                            strBuilder.Append(addressENT.District_Chi);
                        if (!string.IsNullOrEmpty(addressENT.Area_Chi))
                            strBuilder.Append(addressENT.Area_Chi);
                        if (!string.IsNullOrEmpty(addressENT.Street_Rd_Chi))
                            strBuilder.Append(addressENT.Street_Rd_Chi);
                        if (!string.IsNullOrEmpty(addressENT.Flat_Bldg_Chi))
                            strBuilder.Append(addressENT.Flat_Bldg_Chi);
                        
                    }
                }
                str = strBuilder.ToString();
                return str;
            }
        }
        public tblContactInfo infoENT
        {
            get
            {
                if (infoUID > 0)
                {
                    tblContactInfo infoENT = new tblContactInfo();
                    tblContactInfoBLL infoBLL = new tblContactInfoBLL();
                    infoENT.ContactInfoUID = infoUID;
                    infoENT = infoBLL.GetEntity(infoENT);
                    return infoENT;
                }
                return null;
            }
        }
        public string Email
        {
            get
            {
                string str = string.Empty;
                if (infoENT != null)
                {
                    StringBuilder strBuilder = new StringBuilder();
                    if (!string.IsNullOrEmpty(infoENT.Email1))
                        strBuilder.Append(infoENT.Email1).Append(strSpace);
                    if (!string.IsNullOrEmpty(infoENT.Email2))
                        strBuilder.Append(infoENT.Email2).Append(strSpace);
                    if (!string.IsNullOrEmpty(infoENT.Email3))
                        strBuilder.Append(infoENT.Email3).Append(strSpace);
                    if (!string.IsNullOrEmpty(infoENT.Email4))
                        strBuilder.Append(infoENT.Email4).Append(strSpace);
                    if (!string.IsNullOrEmpty(infoENT.Email5))
                        strBuilder.Append(infoENT.Email5).Append(strSpace);
                    str = strBuilder.ToString();
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - strSpace.Length);
                }
                return str;
            }
        }
        public string WebSite
        {
            get
            {
                string str = string.Empty;
                if (infoENT != null)
                {
                    StringBuilder strBuilder = new StringBuilder();
                    if (!string.IsNullOrEmpty(infoENT.Website1))
                        strBuilder.Append(infoENT.Website1).Append(strSpace);
                    if (!string.IsNullOrEmpty(infoENT.Website2))
                        strBuilder.Append(infoENT.Website2).Append(strSpace);
                    str = strBuilder.ToString();
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - strSpace.Length);
                }
                return str;
            }
        }
        public string strTel
        {
            get
            {
                string str = string.Empty;
                if (infoENT != null)
                {
                    StringBuilder strBuilder = new StringBuilder();
                    if (!string.IsNullOrEmpty(infoENT.Tel1))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Tel1_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Tel1_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Tel1_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Tel1_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Tel1).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Tel2))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Tel2_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Tel2_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Tel2_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Tel2_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Tel2).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Tel3))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Tel3_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Tel3_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Tel3_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Tel3_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Tel3).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Tel4))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Tel4_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Tel4_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Tel4_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Tel4_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Tel4).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Tel5))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Tel5_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Tel5_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Tel5_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Tel5_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Tel5).Append(strSpace);
                    }
                    str = strBuilder.ToString();
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - strSpace.Length);
                }
                return str;
            }
        }
        public string strFax
        {
            get
            {
                string str = string.Empty;
                StringBuilder strBuilder = new StringBuilder();
                if (infoENT != null)
                {
                    if (!string.IsNullOrEmpty(infoENT.Fax1))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Fax1_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Fax1_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Fax1_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Fax1_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Fax1).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Fax2))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Fax2_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Fax2_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Fax2_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Fax2_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Fax2).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Fax3))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Fax3_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Fax3_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Fax3_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Fax3_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Fax3).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Fax4))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Fax4_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Fax4_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Fax4_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Fax4_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Fax4).Append(strSpace);
                    }
                    if (!string.IsNullOrEmpty(infoENT.Fax5))
                    {
                        if (!string.IsNullOrEmpty(infoENT.Fax5_AreaCode))
                            strBuilder.Append("(").Append(infoENT.Fax5_AreaCode).Append(") ");
                        if (!string.IsNullOrEmpty(infoENT.Fax5_CountryCode))
                            strBuilder.Append("(").Append(infoENT.Fax5_CountryCode).Append(") ");
                        strBuilder.Append(infoENT.Fax5).Append(strSpace);
                    }
                    str = strBuilder.ToString();
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - strSpace.Length);
                }
                return str;
            }
        }
        public string FullName
        {
            get
            {
                string str = string.Empty;
                if (CPENT != null)
                {
                    str = string.Format("{0} {1} {2} {3} {4} {5}&nbsp;&nbsp;{6} {7}{8} {9}", Salutation, CPENT.ChristianName, CPENT.FirstName_Eng, CPENT.LastName_Eng, CPENT.OtherTitle_Eng, Position_Eng, CPENT.LastName_Chi, CPENT.FirstName_Chi, CPENT.OtherTitle_Chi, Position_Chi);
                }
                return str;
            }
        }
        public string FullNameAndImg {
            get {
                string str = string.Empty;
                if (CPENT != null)
                {
                    tblMediaRoomRelatedBLL relaredbll = new tblMediaRoomRelatedBLL();
                    tblMediaRoom_Related ent = new tblMediaRoom_Related();
                    ent.Category = Constant.MediaRoomCategory.contactperson_photo;
                    ent.RelatedUID = CPENT.ContactPersonUID;
                    ent.Deleted = 0;
                    string strImgUrl = string.Empty;
                    if (relaredbll.GetEntity(ent) != null)
                    {
                        strImgUrl = "<img src=\"/Media/Css/images/cphsmedia.png\" />";
                    }
                    str = string.Format("{0} {1}",FullName, strImgUrl);
                }
                return str;
            }
        }
        public string strJobNature
        {
            get
            {
                string str = string.Empty;
                var jobNature = CPENT.tblCPerson_JobNature.Where(a => a.Deleted == 0);
                StringBuilder strBuilder = new StringBuilder();
                foreach (var temp in jobNature)
                {
                    strBuilder.Append(temp.mtblJobNature.JobNature_Eng);
                    if (!string.IsNullOrEmpty(temp.mtblJobNature.JobNature_Chi))
                        strBuilder.Append("&nbsp;").Append(temp.mtblJobNature.JobNature_Chi);
                    strBuilder.Append(strSpace);
                }
                str = strBuilder.ToString();
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - strSpace.Length);
                return str;
            }
        }
        public string strCommitteeMember
        {
            get
            {
                string str = string.Empty;
                StringBuilder strBuilder = new StringBuilder();
                var committee = CPENT.tblCPerson_CommMember.Where(a=>a.Deleted==0).OrderByDescending(a => a.Cperson_CommMemberUID).FirstOrDefault();
                if (committee != null)
                {
                    var tempType=committee.mtblCommType;
                    var tempPosition=committee.mtblCommPosition;

                    strBuilder.Append(tempType.CommType_Eng);
                    if (!string.IsNullOrEmpty(tempType.CommType_Chi))
                        strBuilder.Append("&nbsp;").Append(tempType.CommType_Chi);
                    strBuilder.Append(strSpace).Append(tempPosition.CommPosition_Eng);
                    if (!string.IsNullOrEmpty(tempPosition.CommPosition_Chi))
                        strBuilder.Append("&nbsp;").Append(tempPosition.CommPosition_Chi);
                }
                str = strBuilder.ToString();
                return str;

            }
        }
        public string strAttribute
        {
            get
            {
                var atttibuteList = CPENT.tblCPerson_Attribute.Where(a => a.Deleted == 0);
                List<int> strlist = new List<int>();
                foreach (var temp in atttibuteList)
                {
                    strlist.Add((int)temp.AttributeUID);
                }
                mtblAttributeBLL bll = new mtblAttributeBLL();
                return bll.GetAttrubuteByUID(strlist);
            }
        }

    }
}
